package com.kaisa.activity.utils;

import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;

import com.kaisa.activity.BaseApp;

import org.xutils.DbManager;

import java.io.File;


public class FileUtils {


    public static File getRootPath() {
        File rootFile = null;
        if (Build.VERSION.SDK_INT >= 14) {
            rootFile = BaseApp.getInstance().getExternalCacheDir();
        } else {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                // sdk/qianfeng/cache
                rootFile = new File(Environment.getExternalStorageDirectory() + File.separator + AppConfig.SDK_ROOT_FILE);
            } else {
                //  sys/data/data/
                rootFile = new File(Environment.getDataDirectory() + File.separator + BaseApp.getInstance().getPackageName());
            }
        }
        if (!rootFile.exists()) {
            rootFile.mkdirs();
        }
        return rootFile;
    }

    //sdk/qianfeng/  down
    public static String getDownPath() {
        File down = new File(getRootPath() + File.separator + AppConfig.DOWN_PATH);
        if (!down.exists()) {
            down.mkdirs();
        }
        return down.getAbsolutePath();
    }


    //sdk/qianfeng/  down
    public static File getDbFile() {
        File dbFile = new File(getRootPath() + File.separator + AppConfig.DB_PATH);
        if (!dbFile.exists()) {
            dbFile.mkdirs();
        }
        return dbFile;
    }

    public static String getFileNameFromUrl(String url) {
        return !TextUtils.isEmpty(url) ? url.substring(url.lastIndexOf("/") + 1) : "";


    }

    public static DbManager.DaoConfig getConfig(){
        DbManager.DaoConfig config = new DbManager.DaoConfig();
        config.setDbDir(FileUtils.getDbFile());
        config.setDbName(AppConfig.DB_NAME);
        config.setAllowTransaction(true);
        config.setDbVersion(AppConfig.DB_VERSION);
        return config;
    }

}
